package cn.com.dhcc.ddi.datamapping.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.datamapping.vo.DataMapping;
import cn.com.dhcc.ddi.datamapping.vo.wapper.DataMappingRowWapper;

/**
 * @日期：2016/11/15 16:14
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class DataMappingDao extends BaseDao {
	
    /**
     * 获取对象
     * @param id
     * @return
     * @throws DaoException
     */
    public DataMapping get(String id) throws DaoException {
        return getDao().getModuleById("获取对象", DataMapping.class, new Object[]{id});
    }

    /**
     * 新增
     * @param obj
     * @return
     * @throws DaoException
     */
    public int insert(DataMapping obj) throws DaoException {
        return getDao().insertModule("新增", obj);
    }

    /**
     * 修改
     * @param obj
     * @return
     * @throws DaoException
     */
    public int update(DataMapping obj) throws DaoException {
        return getDao().updateModuleByIdExecuteNull("修改", obj);
    }

    /**
     * 新增或修改
     * @param obj
     * @return
     * @throws DaoException
     */
    public int insertOrUpdate(DataMapping obj) throws DaoException {
        return getDao().saveOrUpdateModuleByIdExecuteNull("新增或修改", obj);
    }
    
    /**
     * 获取所有映射列表(有效列表)
     * @return 所有映射列表
     * @throws DaoException
     */
    public List<DataMapping> getMappingList() throws DaoException {
    	String msg = "";
		String sql = "select * from  " + SI_TABLE("t_ddi_ds_mapping")+" t1 WHERE status='1'";
		try {
			return getDao().list("", sql, new DataMappingRowWapper(), new Object[]{});
		} catch (DaoException e) {
			msg = "获取所有映射列表失败";
			throw new DaoException(msg, e);
		}
    }
    
	/**
	 * 
	 * @Title: getCountBySrcDsId
	 * @Description: 根据源端数据源id获取映射信息量
	 * @param @param srcDsId
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
    public int getCountBySrcDsId(String srcDsId) throws DaoException{
    	String sql = "select count(*) from  " + SI_TABLE("t_ddi_ds_mapping")+" where src_ds_id = ?";
    	return CM.getDao().queryForInt("根据源端数据源id获取映射信息量", sql, new Object[]{srcDsId});
    }
    
    /**
     * 
     * @Title: getCountByDsId
     * @Description: 根据数据源id获取映射信息量
     * @param @param srcDsId
     * @param @return
     * @param @throws DaoException 参数
     * @return int 返回类型
     * @throws
     */
    public int getCountByDsId(String dsId) throws DaoException{
        String sql = "select count(*) from  " + SI_TABLE("t_ddi_ds_mapping")+" where src_ds_id = ? || dest_ds_id = ? ";
        return CM.getDao().queryForInt("根据源id获取映射信息量", sql, new Object[]{dsId, dsId});
    }
    
	/**
	 * 
	 * @Title: getPagerList
	 * @Description: 获取数据映射信息列表
	 * @param @param pager
	 * @param @param searchParameter
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return PagerData<DataMapping> 返回类型
	 * @throws
	 */
    public PagerData<DataMapping> getPagerList(Pager pager, WhereCondition searchParameter) throws DaoException {
		String sql = "select t1.*, t2.name srcDsName,t3.name destDsName from "
				    +    SI_TABLE("t_ddi_ds_mapping")+" t1 "
				    +" left join "
				    +    SI_TABLE("t_ddi_datasource")+" t2 on t1.src_ds_id=t2.id "
					+" left join "
				    +    SI_TABLE("t_ddi_datasource")+" t3 on t1.dest_ds_id=t3.id ";
		return getDao().getPagerList("获取数据映射信息列表", sql, new DataMappingRowWapper(), pager, searchParameter, new Object[]{});
	}
    
	/**
	 * 
	 * @Title: delete
	 * @Description: 删除数据映射
	 * @param @param id
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
    public int delete(String id) throws DaoException{
    	return CM.getDao().deleteModuleById("删除数据映射", DataMapping.class, new Object[]{id});
    }
    
	/**
	 * 
	 * @Title: getMappingById
	 * @Description: 获取数据源映射对象
	 * @param @param id
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return DataMapping 返回类型
	 * @throws
	 */
    public DataMapping getMappingById(String id) throws DaoException {
		String sql = "select t1.*, t2.name srcDsName,t3.name destDsName from "
				    +    SI_TABLE("t_ddi_ds_mapping")+" t1 "
				    +" left join "
				    +    SI_TABLE("t_ddi_datasource")+" t2 on t1.src_ds_id=t2.id "
					+" left join "
				    +    SI_TABLE("t_ddi_datasource")+" t3 on t1.dest_ds_id=t3.id "
				    +"  where t1.id = ? ";
		return getDao().get("获取数据源映射对象", sql, new DataMappingRowWapper(), new Object[]{id});
	}
    
	/**
	 * 
	 * @Title: count
	 * @Description: 验证名称唯一性
	 * @param @param searchParameter
	 * @param @return
	 * @param @throws DaoException 参数
	 * @return int 返回类型
	 * @throws
	 */
    public int count(WhereCondition searchParameter) throws DaoException {
		String sql = "select count(*) from " + SI_TABLE("t_ddi_ds_mapping");
		return CM.getDao().queryForInt("数据源信息已存在", sql, searchParameter, new Object[]{});
	}
	/**
	 * 获取dm id
	 * @param searchParameter
	 * @return
	 * @throws DaoException
	 */
	public List<String> getDmid(WhereCondition searchParameter) throws DaoException {
		String sql = "select t1.id from "
				+    SI_TABLE("t_ddi_ds_mapping")+" t1 "
				+" left join "
				+    SI_TABLE("t_ddi_datasource")+" t2 on t1.src_ds_id=t2.id "
				+" left join "
				+    SI_TABLE("t_ddi_datasource")+" t3 on t1.dest_ds_id=t3.id ";
		return CM.getDao().listSignleStringColumn("获取dmid", sql, searchParameter, new Object[]{});
	}

	/**
	 * 获取数据映射
	 * @param searchParameter
	 * @return
	 * @throws DaoException
	 */
	public List<DataMapping> getMappingList( WhereCondition searchParameter) throws DaoException {
		String sql = "select t1.* from "
				+    SI_TABLE("t_ddi_ds_mapping")+" t1 "
				+" left join "
				+    SI_TABLE("t_ddi_datasource")+" t2 on t1.src_ds_id=t2.id "
				+" left join "
				+    SI_TABLE("t_ddi_datasource")+" t3 on t1.dest_ds_id=t3.id "
				+" where t1.status='1' ";
		return getDao().listModule("获取数据映射", sql, DataMapping.class, searchParameter);
	}
}
